'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 建筑
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var dataApi = require('../libs/dataApi');
var buildType = require('../consts/buildType');
var cacheUtil = require('../util/cacheUtil.js');

var build_type_conf_cache = {};
/**
 * 获取建筑类型配置
 * @param  {[type]} build_type [description]
 * @return {[type]}            [description]
 */
exports.getBuildTypeConf = function (build_type) {
    if (build_type in build_type_conf_cache) {
        return build_type_conf_cache[build_type];
    }

    var build_type_conf = _getBuildTypeConf(build_type);
    build_type_conf_cache[build_type] = build_type_conf;

    return build_type_conf;
};

var build_lv_conf_cache = {};
/**
 * 获取建筑等级的配置
 * @param  {[type]} build_type [description]
 * @return {[type]}            [description]
 */
exports.getBuildLvConf = function (build_type, lv) {
    var key = build_type * 1000 + lv;
    if (key in build_lv_conf_cache) {
        return build_lv_conf_cache[key];
    }

    var result = null;
    var build_lv_conf = dataApi.building_lvl.findById(build_type);
    if (!build_lv_conf) {
        console.error('build type can not found:' + build_type);
    } else {
        for (var i = 0; i < build_lv_conf.length; i++) {
            var build_lv = build_lv_conf[i];
            if (build_lv.level == lv) {
                result = build_lv;
            }
        }
        if (!result) {
            console.error('build lv can not found:' + lv);
        }
    }
    build_lv_conf_cache[key] = result;
    return result;
};

var build_use_type_cache = {};
/**
 * 获取建筑分类
 */
exports.getBuildUseType = function (build_type) {
    if (build_type in build_use_type_cache) {
        return build_use_type_cache[build_type];
    }

    var build_type_conf = _getBuildTypeConf(build_type);
    var use_type = 0;
    if (!!build_type_conf) {
        use_type = build_type_conf.type;
    }
    build_use_type_cache[build_type] = use_type;
    return use_type;
};

// 获取主城等级对应的可建造塔上限值
exports.getBuildTotalNum = cacheUtil.memorize(function (nexus_lv) {
    var max_num = 0;
    var build_lv_conf = dataApi.building_lvl.findById(buildType.NEXUS);

    if (!build_lv_conf) {
        console.error('build_lv_conf id can not found:' + nexus_lv);
        return 0;
    }

    _.some(build_lv_conf, function (value) {
        if (value.level == nexus_lv) {
            max_num = value.tower_num;
            return true;
        }
        return false;
    });
    return max_num;
});

var cache_build_max_num = {};
/**
 * 获取内城建筑最大可建筑的数量
 * @param  {[type]} nexus_lv   [description]
 * @param  {[type]} build_type [description]
 * @return {[type]}            [description]
 */
exports.getBuildMaxNum = function (nexus_lv, build_type) {
    var key = build_type * 1000 + nexus_lv;
    if (key in cache_build_max_num) {
        return cache_build_max_num[key];
    }

    build_type = parseInt(build_type);
    var nexus_conf = this.getBuildLvConf(buildType.NEXUS, nexus_lv);
    if (!nexus_conf) {
        console.error('nexus can not found:' + nexus_lv);
        cache_build_max_num[key] = 0;
        return 0;
    }

    var build_max_num = 1;
    if (build_type === buildType.MINE) {
        build_max_num = nexus_conf.gold_mine_num;
    } else if (build_type === buildType.FARM) {
        build_max_num = nexus_conf.farm_num;
    }
    cache_build_max_num[key] = build_max_num;

    return build_max_num;
};

var build_attr_map = {};
build_attr_map[buildType.ARROW_TOWER] = 'ballista_towers_num';
build_attr_map[buildType.CANNON_TOWER] = 'gun_tower_num';
build_attr_map[buildType.LIGHT_TOWER] = 'light_tower_num';
build_attr_map[buildType.ICE_TOWER] = 'ice_tower_num';
build_attr_map[buildType.CROSSBOW_TOWER] = 'crossbow_tower_num';
build_attr_map[buildType.DRAGON_TOWER] = 'dragon_tower_num';
build_attr_map[buildType.SNOW_TOWER] = 'snow_tower_num';
build_attr_map[buildType.ELECTRIC_TOWER] = 'electric_tower_num';

var cache_def_build_max_num = {};
/**
 * 获取外城防御建筑最大可建筑的数量
 * @param  {[type]} nexus_lv   [description]
 * @param  {[type]} build_type [description]
 * @return {[type]}            [description]
 */
exports.getDefBuildMaxNum = function (nexus_lv, build_type) {
    build_type = parseInt(build_type);
    var key = build_type * 1000 + nexus_lv;
    if (key in cache_def_build_max_num) {
        return cache_def_build_max_num[key];
    }

    var nexus_conf = this.getBuildLvConf(buildType.NEXUS, nexus_lv);
    if (!nexus_conf) {
        throw new Error('nexus can not found:' + nexus_lv);
    }

    var max_def_build_num = 1;
    var attr_key = build_attr_map[build_type];
    if (!attr_key) {
        console.error('invalid build type:', build_type);
    }
    max_def_build_num = nexus_conf[attr_key];
    cache_def_build_max_num[key] = max_def_build_num;

    return max_def_build_num;
};

/**
 * 获取建筑类型配置
 * @param  {[type]} build_type [description]
 * @return {[type]}            [description]
 */
var _getBuildTypeConf = function (build_type) {
    var build_type_conf = dataApi.building_type.findById(build_type);
    if (!build_type_conf) {
        console.error('build type can not found:' + build_type);
    }
    return build_type_conf;
};

/**
 * 获取建筑星级配置
 * @param  {[type]} build_type [description]
 * @return {[type]}            [description]
 */
exports.getBuildStarConf = function (build_star) {
    var build_star_conf = dataApi.building_star.findById(build_star);
    if (!build_star_conf) {
        console.error('build type can not found:' + build_star);
        return null;
    }
    return build_star_conf;
};
